/*
* @Author: dengjiayao
* @Date:   2017-08-11 09:51:41
* @Last Modified by:   dengjiayao
* @Last Modified time: 2017-08-15 09:40:47
*/
import queryString from 'query-string'
import { showMsg, getExpertCategory, saveExpert, updateExpert, getExpertDetail } from '../../vuex/action'

export default {
  name: 'expert-config',
  data() {
    return {
      actionType: 'ADD', // ADD or UPDATE
      mainCategory: [],
      industryCategory: [],
      form: {
        mainCategoryId: '-1',
        industryCategoryId: '-1',
        name: '',
        company: '',
        position: '',
        phone: '',
        email: '',
        experience: '',
        hisCooperation: '',
        score: '',
        evaluation: '',
        remarks: ''
      }
    }
  },
  vuex: {
    actions: {
      xShowMsg: showMsg,
      xGetCategory: getExpertCategory,
      xSave: saveExpert,
      xUpdate: updateExpert,
      xGetDetail: getExpertDetail
    }
  },
  created() {
    this.xGetCategory()
      .then((data) => {
        this.mainCategory = data.mainCategory
        this.industryCategory = data.industryCategory
      })
    let query = queryString.parse(window.location.search)
    if (query && query.id) {
      this.actionType = 'UPDATE'
      this.xGetDetail({
        id: query.id
      })
        .then((data) => {
          let info = data.expertInfo
          this.form = {
            id: info.id,
            mainCategoryId: info.mainCategoryId,
            industryCategoryId: info.industryCategoryId,
            name: info.name,
            company: info.company,
            position: info.position,
            phone: info.phone,
            email: info.email,
            experience: info.experience,
            hisCooperation: info.hisCooperation,
            score: info.score,
            evaluation: info.evaluation,
            remarks: info.remarks
          }
        })
        .catch((res) => {
          this.xShowMsg({
            type: 'danger',
            content: res.error_msg
          })
        })
    }
  },
  methods: {
    valid() {
      let form = this.form
      let selectFields = ['mainCategoryId', 'industryCategoryId']
      for (let key of selectFields) {
        if (form[key] < 0) {
          return Promise.reject({
            error_msg: '* 标识的为必填项'
          })
        }
      }
      let requireFields = ['name', 'company', 'position']
      for (let key2 of requireFields) {
        if (form[key2].length === 0) {
          return Promise.reject({
            error_msg: '* 标识的为必填项'
          })
        }
      }
      return Promise.resolve()
    },
    submit() {
      this.valid()
        .then(() => {
          if (this.actionType === 'ADD') {
            return this.xSave(this.form)
          } else {
            return this.xUpdate(this.form)
          }
        })
        .then(() => {
          window.location.href = 'list.html'
        })
        .catch((res) => {
          this.xShowMsg({
            type: 'danger',
            content: res.error_msg
          })
        })
    }
  }
}
